<?php

class Links extends Model_Abstract
{
	protected $_table_name = table_links;

	public function insert( $user_id, $category_id, $title, $content, $url = '', $url_title='', $tags = '' )
    {
		$_modified = date( 'Y-m-d H:i:s', time() );

		$_link = array(
			'link_author'   => $user_id,
			'link_votes'    => 1,
			'link_karma'    => 1,
			'link_modified' => $_modified,
			'link_date'     => $_modified,
			'link_published_date' => $_modified,
			'link_category' => $category_id,
			'link_lang'     => 1,
			'link_url'      => $url,
			'link_url_title'=> $url_title,
			'link_title'    => $title,
			'link_title_url'=> $this->translit( $title ),
			'link_content'  => $content,
			'link_summary'  => $content,
			'link_tags'     => $tags,
			'link_status'   => 'published',

			'link_randkey' => rand(1000000,100000000)
		);

		$fields = $this->_paramsToString( $_link );

		$sql = "INSERT INTO " . $this->_table_name . " SET " . $fields;
		$this->_db->get_results($sql);

		$link_id = $this->_getLastInsertId();
		if( !$link_id )
			$link_id = 1;

		$sql = "UPDATE " . table_totals . " SET total=total+1 WHERE name='published'";
		$this->_db->get_results($sql);

		$sql = "SELECT MAX(vote_karma) as karma FROM `". table_votes ."` WHERE vote_user_id=" . $user_id;
		$votes = $this->_db->get_row($sql);

		$_votes = array(
			'vote_type'    => 'links',
			'vote_date'    => $_modified,
			'vote_link_id' => $link_id,
			'vote_user_id' => $user_id,
			'vote_value'   => 10,
			'vote_karma'   => $votes->karma + 2,
			'vote_ip'      => $_SERVER['REMOTE_ADDR'],
		);
		$fields = $this->_paramsToString( $_votes );
		$sql = "INSERT INTO " . table_votes . " SET " . $fields;
		$this->_db->get_results($sql);

		return $link_id;
    }
}